<?php
/**
 * $Horde: groo/view.php,v 1.27.2.7 2006/03/03 23:00:29 chuck Exp $
 *
 * Copyright 2001-2006 Jon Parise <jon@horde.org>
 *
 * See the enclosed file LICENSE for license information (ASL). If you
 * did not receive this file, see http://www.horde.org/licenses/asl.php.
 *
 * @author  Jon Parise <jon@horde.org>
 * @since   Groo 1.0
 * @package Groo
 */

@define('GROO_BASE', dirname(__FILE__));
require_once GROO_BASE . '/lib/base.php';

/* We can either have a UID or a item id and a collection. Check for UID
 * first. */
if ($uid = Util::getFormData('uid')) {
    $storage = &Groo_Driver::singleton();
    $item = $storage->getByUID($uid);
    if (is_a($item, 'PEAR_Error')) {
        header('Location: ' . Horde::applicationUrl('list.php', true));
        exit;
    }

    $item_id = $item['item_id'];
    $itemlist_id = $item['itemlist_id'];
} else {
    /* If we aren't provided with a item and itemlist, redirect to
     * list.php. */
    $item_id = Util::getFormData('item');
    $itemlist_id = Util::getFormData('itemlist');
    if (!isset($item_id) || !$itemlist_id) {
        header('Location: ' . Horde::applicationUrl('list.php', true));
        exit;
    }

    /* Get the current item. */
    $item = Groo::getItem($itemlist_id, $item_id);
}

$share = $GLOBALS['groo_shares']->getShare($itemlist_id);
if (is_a($share, 'PEAR_Error')) {
    $notification->push(sprintf(_("There was an error viewing this collection: %s"), $share->getMessage()), 'horde.error');
    header('Location: ' . Horde::applicationUrl('list.php', true));
    exit;
} elseif (!$share->hasPermission(Auth::getAuth(), PERMS_READ)) {
    $notification->push(sprintf(_("You do not have permission to view the collection %s."), $share->get('name')), 'horde.error');
    header('Location: ' . Horde::applicationUrl('list.php', true));
    exit;
}

/* If the requested item doesn't exist, display an error message. */
if (!$item || !isset($item['item_id'])) {
    $notification->push(_("Item not found."), 'horde.error');
    header('Location: ' . Horde::applicationUrl('list.php', true));
    exit;
}

/* Get the item's history. */
if (!empty($item['uid'])) {
    $history = &Horde_History::singleton();
    $log = $history->getHistory('groo:' . $itemlist_id . ':' . $item['uid']);
    if ($log && !is_a($log, 'PEAR_Error')) {
        foreach ($log->getData() as $entry) {
            switch ($entry['action']) {
            case 'add':
                $created = $entry['ts'];
                break;

            case 'modify':
                $modified = $entry['ts'];
                break;
            }
        }
    }
}

/* Set the page title to the current item's name, if it exists. */
$title = (!$item) ? $item['desc'] : _("Item Details");
$print_view = (Util::getFormData('print') == 'true');
if (!$print_view) {
    Horde::addScriptFile('popup.js', 'horde', true);
}
require GROO_TEMPLATES . '/common-header.inc';

if ($print_view) {
    require $registry->get('templates', 'horde') . '/javascript/print.js';
} else {
    $print_link = Util::addParameter('view.php', array('item' => $item_id,
                                                       'itemlist' => $itemlist_id,
                                                       'print' => 'true'));
    $print_link = Horde::url($print_link);
    Horde::addScriptFile('stripe.js', 'horde', true);
    require GROO_TEMPLATES . '/menu.inc';
}

require GROO_TEMPLATES . '/view/item.inc';
require $registry->get('templates', 'horde') . '/common-footer.inc';
